@extends('admin.layout.layout', ['row' => @$page])
@section('style')
    <link rel="stylesheet" href="/dist/element-ui.css">
    <style>
        .transferSelectorGroup{
            display: flex;
            flex-wrap: nowrap;
            overflow: hidden;
            height: 440px;
            border: 1px solid #eee;
            padding: 0;
            box-sizing: border-box;
        }
        .candidateListBox{
            flex: 0 0 50%;
            padding: 15px;
            position: relative;
        }
        .selectedListBox{
            flex: 0 0 50%;
            padding: 15px;
            position: relative;
            border-left: 1px solid #eee;
        }
        .listScrollBox{
            margin: 10px 0;
            overflow: auto;
            position: absolute;
            bottom: 0;
            left: 15px;
            right: 15px;
            top: 40px;
        }
        .candidateListBox .listScrollBox{
            top: 70px;
        }
        .listUl .item {
            padding: 10px 0;
        }
        .listUl .item.choosed {
            background-color: #eeeeee;
        }
        .listUl .item + .item{
            border-top: 1px solid #eeeeee;
        }
        .listUl .box{
            display: flex;
            flex-wrap: nowrap;
            height: 13px;
            font-size: 13px;
            line-height: 13px;
            padding-left: 10px;
            padding-right: 10px;
        }
        .listUl .roll_number{
            flex: 0 0 auto;
            text-align: left;
        }
        .listUl .length{
            flex: 1;
            text-align: right;
        }
        .listUl .delete{
            flex: 1;
            text-align: right;
        }
        .listUl .delete .del{
            cursor: pointer;
        }
        .listUl li:hover .del{
            color: #f00;
        }
        .selectedListBox .listUl li:hover{
            background-color: #eeeeee;
        }
        .table.vtop td, .table.vtop th, .vtop td, .vtop th{
            vertical-align: top;
        }
        .precut-view .el-dialog__header{
            text-align: center;
        }
        .warning {
            margin-left: 5px;
            color: red;
        }
    </style>
@endsection
@section('contain')
    <style>
    </style>
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <div id="vueRoot">
            <table class="table s3 vtop">
                <tbody>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt">
                        <div>
                            <table class="table table-s3">
                                <thead>
                                <tr>
                                    <th style="width: 10%">{{ ts('KYMXH') }}</th>
                                    <th style="width: 10%">{{ ts('ZTBW') }}</th>
                                    <th style="width: 10%">{{ ts('ylcd2') }}</th>
                                    <th style="width: 10%">{{ ts('SCSL') }}</th>

                                    <th style="width: 10%">{{ ts('rollnumber') }}</th>
                                    <th style="width: 10%">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="(form, index) in forms" :key="index" class="text-center">

                                    <td>
                                        <el-select v-model="form['film_type_id']" placeholder="请选择" clearable @change="changeFilmType($event, index)">
                                            <el-option
                                                    v-for="o in filmTypes"
                                                    :key="o.value"
                                                    :label="o.label"
                                                    :value="o.value">
                                            </el-option>
                                        </el-select>
                                        <div class="text-danger" v-if="form['restlen'] != null">剩余 <b v-text="form['restlen']"></b> cm</div>
                                    </td>
                                    <td>
                                        <el-select v-model="form['part_id']" placeholder="请选择" clearable @change="changePart($event, index)">
                                            <el-option
                                                    v-for="o in parts"
                                                    :key="o.value"
                                                    :label="o.label"
                                                    :value="o.value">
                                            </el-option>
                                        </el-select>
                                    </td>
                                    <td>
                                        <el-input v-model="form['length']" type="number" readonly></el-input>
                                    </td>
                                    <td>
                                        <el-input v-model="form['number']" type="number"></el-input>
                                        <div class="text-danger" v-if="form['number']>0 && form['length']>0">用料总长度 <b v-text="form['number'] * form['length']"></b> cm</div>
                                    </td>
                                    <td>
                                        <div v-for="(ir, ij) in form['roll_numbers']" :key="ij" v-text="ir"></div>
                                    </td>
                                    <td>
                                        <el-button type="primary" size="mini" @click="selectRolls(index)">编辑</el-button>
                                        <!--<el-button type="danger" size="mini" @click="doDelete(index)">删除</el-button>-->
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                            {{--<div class="my-5 pl-5">
                                <el-button type="primary" @click="addMore">{{ ts('addmore') }}</el-button>
                            </div>--}}
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <el-button type="primary" disabled v-if="isSubmit">{{ ts('submit') }}</el-button>
                <el-button type="primary" @click="handleSubmit" v-else>{{ ts('submit') }}</el-button>
            </div>

            <el-dialog title="编辑" :visible.sync="dialogVisible" width="45%" :before-close="handleClose" :show-close=false>
                <div class="transferSelectorGroup">
                    <div class="candidateListBox">
                        <h6>可用卷号：（共 <b v-text="rollSelectList.length"></b> 个）</h6>
                        <el-input size="mini" v-model="rollSearchText"></el-input>
                        <div class="listScrollBox">
                            <ul class="listUl">
                                <li v-for="(o, index) in filterRollsList()" :key="index" class="item" :class="{choosed: isSelected(o) }" @click="chooseRoll(o)">
                                    <div class="box">
                                        <span v-text="o['roll_number']" class="roll_number"></span>
                                        <span v-text="o['restlen']" class="length"></span>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="selectedListBox">
                        <h6>已选择卷号：（共 <b v-text="rollSelectedLength"></b> cm）</h6>
                        <div class="listScrollBox">
                            <ul class="listUl">
                                <li v-for="(o, index) in rollSelectedList" :key="index" class="item">
                                    <div class="box">
                                        <span v-text="o.roll_number" class="roll_number"></span>
                                        <span v-text="o.warning" class="warning"></span>
                                        <div class="delete">
                                            <i class="el-icon-circle-close del" @click="deleteRoll(index)"></i>
                                        </div>

                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleSelectRolls">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog title="套件预览" :visible.sync="confirmVisible" width="60%" :before-close="handleClose" :show-close=false class="precut-view">
                <div>
                    <el-table :data="tableList" style="width: 100%">
                        <el-table-column type="index" label="序号"></el-table-column>
                        <el-table-column prop="precut_id" label="套件ID"></el-table-column>
                        <el-table-column prop="part" label="装贴部位"></el-table-column>
                        <el-table-column prop="length" label="用料长度（cm）"></el-table-column>
                        <el-table-column prop="filmType" label="膜型号"></el-table-column>
                        <el-table-column prop="roll_number" label="膜卷号">
                            <template slot-scope="scope">
                                <div v-for="(item, index) in scope.row.roll_numbers" :key="index">
                                    <span v-text="item.roll_number"></span>：
                                    <span v-text="item.length"></span> cm
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="handleConfirmCancel">取 消</el-button>
                    <el-button type="primary" @click="handleConfirmConfirm">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>


@endsection

@section('script')
    {{--<script src="/dist/vue.js"></script>--}}
    <script src="/dist/vue.js"></script>
    <script src="/dist/element-ui.js"></script>
    <script></script>
    <script>

        function getPart() {
            var data = new_precut_json_data.part;
            res = [];
            for (var i = 0; i < data.length; i++) {
                var o = data[i];
                if (o.value) {
                    res.push(o)
                }
            }
            return res;
        }
        function getProductType() {
            var data = new_precut_json_data.product_type;
            res = [];
            for (var i = 0; i < data.length; i++) {
                var o = data[i];
                if (o.value) {
                    res.push(o)
                }
            }
            return res;
        }
        function getAllFilmTypes() {
            return coverages_data.filmtype;
        }
        function getFilmTypesV2() {
            var data = coverages_data.filmtype;
            res = [];
            for (var i = 0; i < data.length; i++) {
                var o = data[i];
                if (o.value) {
                    var label = o.label;
                    label = String(label).toUpperCase();
                    if (label.indexOf('ASP') == 0) {
                        res.push(o)
                    }
                }
            }
            return res;
        }
        function getFilmTypes(product_type_id) {
            var data = coverages_data.filmtype;
            res = [];
            for (var i = 0; i < data.length; i++) {
                var o = data[i];
                if (o.value && product_type_id == o.product_type_id) {
                    var label = o.label;
                    label = String(label).toUpperCase();
                    if (label.indexOf('ASP') == 0) {
                        res.push(o)
                    }
                }
            }
            return res;
        }
        var app = new Vue({
            el: '#vueRoot',
            data: {
                roll_numbers: [],
                forms: [],
                form: {
                    part_id: null,
                    length: null,
                    numbers: null,
                    product_type_id: null,
                    film_type_id: null,
                },
                parts: [],
                productTypes: [],
                dialogVisible: false,
                loading: false,
                url: '/admin/ajax/newPrecut',
                rollSelectList: [],
                rollSelectedList: [],
                rollSelectedLength: 0,
                rollSearchText: null,
                formsIndex: null,
                confirmVisible: false,
                tableList: [],
                isSubmit: false,
                isConfirmSubmit: false,
                temporary_id: null,
                filmTypes: getFilmTypesV2(),
            },
            watch: {
                rollSelectedList(val) {
                    let selectedValue = [];
                    for (let i in val) {
                        selectedValue.push(val[i].roll_number);
                    }
                    var that = this;
                    that.rollSelectedLength = 0;
                    var form = this.forms[this.formsIndex] || {};
                    formLength = form['number'] * form['length'];
                    let allLength = 0;
                    for (let i in that.rollSelectedList) {
                        let selectedRoll = that.rollSelectedList[i];
                        for (let j in that.rollSelectList) {
                            let selectData = that.rollSelectList[j];
                            if (selectedRoll.roll_number == selectData.roll_number) {
                                allLength += selectData.restlen;
                            }
                        }
                        this.rollSelectedList[i].warning = allLength < formLength ? '' : '所选膜的用料长度已经足够';
                    }
                    that.rollSelectedLength = allLength
                }
            },
            mounted() {
                this.$nextTick(function () {
                    this.parts = getPart();
                    this.productTypes = getProductType();
                    this.forms.push({})
                })
            },
            methods: {
                handleConfirmConfirm() {
                    var that = this;
                    var f = {
                        t: 'saveProd',
                        temporary_id: this.temporary_id,
                    };
                    if (this.isConfirmSubmit) {
                        this.$message.warning('数据正在提交中……');
                        return false;
                    }
                    this.isConfirmSubmit = true;
                    request.post(this.url, f)
                        .then(d => {
                            this.$message.success(d.msg || '提交成功！');
                            window.location.assign('/admin/new-precut/');
                        })
                        .catch(d => {
                            console.log('catch', d)
                            this.$message.error(d.msg || '系统错误，请稍后重试');
                            that.isConfirmSubmit = false;
                        })

                },
                handleConfirmCancel() {
                    var that = this;
                    msg = '是否确认取消？取消后重新添加套件ID将会修改';
                    this.$confirm(msg, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        that.confirmVisible = false;
                    }).catch(() => {

                    });
                },
                filterRollsList() {
                    var search = this.rollSearchText;
                    console.log(this.rollSearchText)
                    var res = [];
                    var list = this.rollSelectList;

                    if (!search) {
                        return list;
                    }
                    this.rollSelectList.forEach(o => {
                        var roll_number = o['roll_number'];
                        if (roll_number.indexOf(search) >= 0) {
                            res.push(o)
                        }
                    });
                    return res;
                },
                isSelected(o) {
                    var roll_number = o['roll_number']
                    for (let i in this.rollSelectedList) {
                        if (this.rollSelectedList[i].roll_number == roll_number) {
                            console.log(roll_number);
                            return true;
                        }
                    }
                    return false
                },
                addMore() {
                    this.forms.push({})
                },
                doDelete(index) {
                    if (this.forms.length > 1) {
                        this.forms.splice(index, 1);
                    }
                },
                changePart(value, index) {
                    var that = this;
                    this.forms.forEach((form, index) => {
                        var p = {
                            length: this.getPartLength(form.part_id)
                        };
                        form = Object.assign({}, form, p);
                        that.forms.splice(index, 1, form);
                    });
                },
                changeProduct(value, index) {
                    var that = this;
                    var filmTypes = getFilmTypes(value);
                    var p = {
                        filmTypes: filmTypes,
                        film_type_id: null
                    };
                    var form = this.forms[index];
                    form = Object.assign({}, form, p);
                    that.forms.splice(index, 1, form);
                },
                changeFilmType(value, index) {
                    var that = this;
                    var params = {
                        t: 'getRolls',
                        film_type_id: value,
                    };
                    request.post(this.url, params)
                        .then(function (d) {
                            var data = d.data;
                            console.log('params', data);
                            form = that.forms[index];
                            p = {
                                restlen: data.restlen,
                                roll_numbers: [],
                            };
                            form = Object.assign({}, form, p);
                            that.forms.splice(index, 1, form);
                        })
                        .catch(function (d) {
                            //that.$message.error('服务器连接失败，请稍后重试！');
                        });
                },
                chooseRoll(obj) {
                    let hasSelectedRollNumbers = [];
                    for (let i in this.rollSelectedList) {
                        hasSelectedRollNumbers.push(this.rollSelectedList[i].roll_number);
                    }
                    if (hasSelectedRollNumbers.indexOf(obj.roll_number) < 0) {
                        this.rollSelectedList.push({roll_number : obj.roll_number});
                    }
                    console.log(this.rollSelectedList, this.formsIndex)
                },
                deleteRoll(index) {
                    this.rollSelectedList.splice(index, 1);
                },
                selectRolls(index) {
                    var that = this;
                    var form = that.forms[index];
                    var film_type_id = form.film_type_id;
                    this.formsIndex = index;
                    var rollSelectedList = form['roll_numbers'] || [];
                    console.log('rollSelectedList', rollSelectedList);
                    if (!film_type_id) {
                        this.$message.error('请向选择模型号');
                        return false;
                    }
                    var params = {
                        t: 'getRolls',
                        film_type_id: film_type_id,
                    };
                    that.rollSelectedLength = 0;
                    request.post(this.url, params)
                        .then(function (d) {
                            var data = d.data;
                            var list = data.data;
                            that.rollSelectedLength = 0;
                            for (let i in rollSelectedList) {
                                that.rollSelectedList[i].roll_number = rollSelectedList[i];
                            }
                            that.rollSelectList = list;
                            that.rollSelectList.forEach(o => {
                                var roll_number = o['roll_number'];
                                for (let i in that.rollSelectedList) {
                                    console.log();
                                    if (that.rollSelectedList[i].roll_number == roll_number) {
                                        that.rollSelectedLength = that.rollSelectedLength + o.restlen;
                                    }
                                }
                            });
                            that.dialogVisible = true;
                        })
                        .catch(function (d) {
                            console.log('catch',d)
                        });
                },
                handleSelectRolls() {
                    var index = this.formsIndex;
                    var form = this.forms[index];
                    let select_roll_numbers = [];
                    for (let i in this.rollSelectedList) {
                        select_roll_numbers.push(this.rollSelectedList[i].roll_number);
                    }
                    console.log(select_roll_numbers);
                    var p = {
                        roll_numbers: select_roll_numbers
                    };
                    form = Object.assign({}, form, p);
                    this.forms.splice(index, 1, form);
                    this.dialogVisible = false;
                },
                handleClose() {},
                handleSubmit() {
                    var that = this;
                    var errors = [
                        '装贴部位 不能为空',
                        '用料长度 不能为空',
                        '生成数量 不能为空',
                        '产品类型 不能为空',
                        '膜型号 不能为空',
                        '提交内容 不能为空！',
                    ];
                    var forms = this.forms;
                    if (forms.length < 1) {
                        this.$message.error(errors[5]);
                        return false;
                    }
                    var uploads = [];
                    for (var i = 0; i < forms.length; i++) {
                        var form = forms[i];
                        if (!form.part_id || form.part_id < 1) {
                            this.$message.error(errors[0]);
                            return false;
                        }
                        if (!form.length || form.length < 1) {
                            this.$message.error(errors[1]);
                            return false;
                        }
                        if (!form.number || form.number < 1) {
                            this.$message.error(errors[2]);
                            return false;
                        }
                       /* if (!form.product_type_id || form.product_type_id < 1) {
                            this.$message.error(errors[3]);
                            return false;
                        }*/
                        if (!form.film_type_id || form.film_type_id < 1) {
                            this.$message.error(errors[4]);
                            return false;
                        }
                        uploads.push({
                            part_id: form.part_id,
                            length: form.length,
                            number: form.number,
                            product_type_id: form.product_type_id,
                            film_type_id: form.film_type_id,
                            roll_numbers: form.roll_numbers,
                        })
                    }
                    var f = {
                        t: 'saveTemporary',
                        data: uploads,
                    };
                    if (this.isSubmit) {
                        this.$message.warning('数据正在提交中……');
                        return false;
                    }
                    this.isSubmit = true;
                    this.$confirm('如果所选择卷号剩余长度不足，则是用系统默认卷号', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        request.post(this.url, f)
                            .then(d => {
                                console.log('then', d)
                                that.makeTableData(d.data);
                                that.temporary_id = d.data.temporary_id
                                that.isSubmit = false;
                            })
                            .catch(d => {
                                console.log('catch', d)
                                this.$message.error(d.msg || '系统错误，请稍后重试');
                                that.isSubmit = false;
                            })
                    }).catch(() => {
                        this.$message.info('已取消')
                        that.isSubmit = false;
                    });
                    console.log('forms', uploads)
                },
                makeTableData(data) {
                    var coverages = data.precut_coverage;
                    var fparts = data.precut_fpart;
                    var parts = getPart();
                    var filmTypes = getAllFilmTypes();
                    var Parts = [],FilmTypes = [];
                    for (let i = 0; i < parts.length; i++) {
                        var o = parts[i];
                        if (o.value) {
                            Parts[o.value] = o.label
                        }
                    }
                    for (let i = 0; i < filmTypes.length; i++) {
                        var o = filmTypes[i];
                        if (o.value) {
                            FilmTypes[o.value] = o.label
                        }
                    }
                    var forms = {};
                    for (var i = 0; i < fparts.length; i++) {
                        var o = fparts[i];
                        forms[o['precut_id']] = {
                            film_type_id: o['film_type_id'],
                            part_id: o['part_id'],
                            part: Parts[o['part_id']],
                            filmType: FilmTypes[o['film_type_id']],
                            precut_id: o['precut_id'],
                            length: o['length'],
                        }
                    }
                    var Cov = {};
                    for (var i = 0; i < coverages.length; i++) {
                        var o = coverages[i];
                        if (!Cov[o['precut_id']]) {
                            Cov[o['precut_id']] = [];
                        }
                        Cov[o['precut_id']].push(o)
                    }
                    var list = [];
                    for (var o in forms) {
                        var item = forms[o];
                        var rolls = Cov[o] || [];
                        item['roll_numbers'] = rolls;
                        list.push(item);
                    }
                    this.tableList = list;
                    this.confirmVisible = true;

                },
                getPartLength(part_id) {
                    var length = null;
                    this.parts.forEach(part => {
                        if (part.value == part_id) {
                            length = part.min_length;
                        }
                    });
                    return length;
                },
            }
        });



        var filmTypeRlen = {};
        function updateRestlen() {
            var items = $('.new-precut-coverage').find('.item');
            for (var i = 0; i <= items.length - 1; i++) {
                var fkey = items.eq(i).find('[name="film_type_id[]"]').val();
                console.log('adsasd', items.length, fkey)
                if (fkey) {
                    var restlen = filmTypeRlen[fkey];
                    if (restlen !== undefined) {
                        items.eq(i).find('.residue-length b').text(restlen)
                        items.eq(i).find('.residue-length').removeClass('d-none')
                    }
                } else {
                    items.eq(i).find('.residue-length').addClass('d-none')
                }
            }
        }
        /*
        * 根据模型号获取剩余长度*/
        function getRestlen(film_type_id) {
            var form = {
                'film_type_id': film_type_id
            };
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'getRestlen',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var data = d.data;
                    filmTypeRlen[data['film_type_id']] = data['restlen'];
                    updateRestlen();
                })
                .catch(function (d) {

                })
        }
        /*
        * 单部位*/
        function savePrecut(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.new-precut-form');
            var state = false;
            var msg;
            var a = form.part_id;
            var b = form.length;
            var c = form.product_type_id;
            var d = form.film_type_id;
            var e = form.number;
            if ((!a || !Array.isArray(a))) {
                a = [];
                if (!msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            if ((!b || !Array.isArray(b))) {
                b = [];
                if (!msg) {
                    msg = '用料长度 不能为空';
                }
            }
            if ((!e || !Array.isArray(e))) {
                b = [];
                if (!msg) {
                    msg = '生成数量 不能为空';
                }
            }
            if ((!c || !Array.isArray(c))) {
                c = [];
                if (!msg) {
                    msg = '产品类型 不能为空';
                }
            }
            if ((!d || !Array.isArray(d))) {
                d = [];
                if (!msg) {
                    msg = '膜型号 不能为空';
                }
            }
            for (var i = 0; i < a.length; i++) {
                if (!a[i] && !msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            for (var i = 0; i < b.length; i++) {
                if (!b[i] && !msg) {
                    msg = '用料长度 不能为空';
                }
            }
            for (var i = 0; i < e.length; i++) {
                if (!e[i] && !msg) {
                    msg = '生成数量 不能为空';
                }
            }
            for (var i = 0; i < c.length; i++) {
                if (!c[i] && !msg) {
                    msg = '产品类型 不能为空';
                }
            }
            for (var i = 0; i < d.length; i++) {
                if (!d[i] && !msg) {
                    msg = '膜型号 不能为空';
                }
            }
            if (msg) {
                alert(msg);
                $(dom).attr('disabled', false);
                return false;
            }
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'store',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作成功！';
                    alert(msg);
                    window.location.assign('/admin/new-precut/');
                    return false;
                })
                .catch(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作失败，请重新尝试！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    return false;
                })
        }
        function savePrecutV1(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.new-precut-form');
            var state = false;
            var msg;
            console.log(form);
            if (!form.number || form.number < 1) {
                msg = '生成数量 不能为空';
            }
            var a = form.part_id;
            var b = form.length;
            var c = form.product_type_id;
            var d = form.film_type_id;
            if ((!a || !Array.isArray(a))) {
                a = [];
                if (!msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            if ((!b || !Array.isArray(b))) {
                b = [];
                if (!msg) {
                    msg = '用料长度 不能为空';
                }
            }
            if ((!c || !Array.isArray(c))) {
                c = [];
                if (!msg) {
                    msg = '产品类型 不能为空';
                }
            }
            if ((!d || !Array.isArray(d))) {
                d = [];
                if (!msg) {
                    msg = '膜型号 不能为空';
                }
            }
            for (var i = 0; i < a.length; i++) {
                if (!a[i] && !msg) {
                    msg = '装贴部位 不能为空';
                }
            }
            for (var i = 0; i < b.length; i++) {
                if (!b[i] && !msg) {
                    msg = '用料长度 不能为空';
                }
            }
            for (var i = 0; i < c.length; i++) {
                if (!c[i] && !msg) {
                    msg = '产品类型 不能为空';
                }
            }
            for (var i = 0; i < d.length; i++) {
                if (!d[i] && !msg) {
                    msg = '膜型号 不能为空';
                }
            }
            if (msg) {
                alert(msg);
                $(dom).attr('disabled', false);
                return false;
            }
            var url = '/admin/ajax/newPrecut';
            var params = {
                t: 'store',
            };
            form = Object.assign({}, form, params);
            request.post(url, form)
                .then(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作成功！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    window.location.assign('/admin/new-precut/');
                    console.log(456)
                    return false;
                })
                .catch(function (d) {
                    var msg = (d && d.data && d.data.msg) || (d && d.msg) || '操作失败，请重新尝试！';
                    $(dom).attr('disabled', false);
                    alert(msg);
                    console.log(d)
                    console.log(789)
                    return false;
                })
        }

        function add() {
            var part_json = new_precut_json_data.part;
            var product_json = new_precut_json_data.product_type;

            var part = '<select name="part_id[]" class="form-control part">'+makeSelectOptionsOnly(part_json)+'</select>';
            var filmtype = '<select name="film_type_id[]" class="form-control film_type">'+makeSelectOptions([])+'</select>';
            filmtype += '<div class="residue-length mt-1 d-none text-danger">剩余 <b></b> 厘米</div>';
            var length = '<input type="number" name="length[]" class="form-control">';
            var number = '<input type="number" name="number[]" class="form-control">';
            var product = '<select name="product_type_id[]" class="form-control product_type">'+makeSelectOptionsOnly(product_json)+'</select>';
            var string = '';
            string = '<tr class="item">';
            string += '<td>' + part + '</td>';
            string += '<td>' + length + '</td>';
            string += '<td>' + number + '</td>';
            string += '<td>' + product + '</td>';
            string += '<td>' + filmtype + '</td>';

            var len = $('.new-precut-coverage table tbody tr').length;
            if (len >= 1) {
                string += '<td><button type="button" class="btn btn-danger btn-sm delete">'+ts('delete')+'</button></td>';
            }  else {
                string += '<td></td>';
            }
            $('.new-precut-coverage table tbody').append(string);
        }

        function Init() {
            add();
        }
        $(document).ready(function () {
            Init();
            $('.new-precut-coverage table tbody').on('click', 'tr.item .delete',function () {
                var len = $('.new-precut-coverage table tbody tr').length;
                console.log('sdf');
                if (len > 1) {
                    $(this).parents('.new-precut-coverage table tbody tr.item').remove();
                }  else {
                }
            });
            $('.new-precut-coverage table tbody').on('change', 'tr.item .product_type',function () {
                var item = $(this).parents('.new-precut-coverage table tbody tr.item');
                var film_type = item.find(' .film_type');
                console.log(film_type.length)
                if (film_type.length>=1) {
                    var val = $(this).val();
                    var ft = coverages_data && coverages_data.filmtype;
                    var arr = [];
                    for (var i = 0; i < ft.length; i++) {
                        var item = ft[i];
                        if (item && item.product_type_id && item.product_type_id==val) {
                            arr.push(item)
                        }
                    }
                    film_type.empty().append(makeSelectOptions(arr))
                }
            });
            $('.new-precut-coverage table tbody').on('change', 'tr.item .film_type',function () {
                var val = $(this).val();
                console.log(val)
                if (val) {
                    getRestlen(val);
                } else {
                    updateRestlen();
                }
            });
        })


    </script>
@endsection